# -*- coding:utf8 -*-

from AcideAmine import AcideAmine
from Score import Score
from initADN import *
from initAcideAmine import *
from initListeAlleleToAA import *

class Main:

    def __init__(self, in_score):
        self._ADN = None
        self._Score = in_score

        #Initialisation des listes
        self.listeAcideAmine = []
        self.listeAlleleToAcideAmine = []

        #Codon start
        self.start = AcideAmine('start')
        self.start.addCodon('AUG')
        self.start.setCondonDefault('AUG')

        #Codon stop
        self.stop = AcideAmine('stop')
        self.stop.addCodon('UAA')
        self.stop.addCodon('UAG')
        self.stop.addCodon('UGA')
        self.stop.setCondonDefault('UAA')
        
        self.initListAcideAmine(initListeAcideAmine())
        self.initADN(initADN(self._Score))
        self.initListeAlleleToAcideAmine(initListeADN(self._ADN, self.listeAcideAmine))

    def initListAcideAmine(self, liste):
        self.listeAcideAmine = liste

    def initADN(self, adn):
        self._ADN = adn
        self._ADN.initListeAllele()

    def initListeAlleleToAcideAmine(self, listeAlleleToAcideAmine):
        self.listeAlleleToAcideAmine = listeAlleleToAcideAmine

    def getListeAlleleToAA(self):
        return self.listeAlleleToAcideAmine

    def getADN(self):
        return self._ADN

    def getListeAcideAmine(self):
        return self.listeAcideAmine

    def code(self, in_allele):
        listeOfAA = self.getAAFromAll(in_allele.getId())
        print listeOfAA
        code = []
        for AA in listeOfAA:
            code.append(self.listeAcideAmine[AA].code())
        return code

    def getAAFromAll(self, in_id):
        for tuple in self.listeAlleleToAcideAmine:
            if in_id == tuple[0]:
                return tuple[1]

    def createADN(self, in_codons):
        ADN = self.start.code()
        ADN = ADN + ''.join(map(str, in_codons))
        ADN = ADN + self.stop.code()
        return ADN

    def getAcideAmineCode(self, in_codon):
        for aa in self.listeAcideAmine:
            if in_codon in aa:
                return aa

    def parse(self, in_brin):
        brin = [in_brin[3 * k:3 * (k + 1)] for k in range(len(in_brin) / 3)]
        return brin

    def separe(self, in_brin):
        separe = []
        allele = False
        for codon in in_brin:
            if allele == True:
                if codon in self.stop:
                    allele = False
                    separe.append(part)
                else:
                    part.append(codon)
            else:
                if codon in self.start:
                    allele = True
                    part = []
        return separe

    def searchForAA(self, separe):
        final = []
        for codons in separe:
            final2 = []
            for codon in codons:
                for aa in self.listeAcideAmine.values():
                    if codon in aa:
                        final2.append(aa)
            final.append(final2)
        return final

    def correspond(self, listeAA):
        for aa in listeAA:
            for all in self.listeAlleleToAcideAmine:
                if aa == all[1]:
                    print all[0]
